<?php

namespace App\Admin\Controllers;

use App\Models\CollArea;
use App\Models\CollPosition;
use App\Models\CompanyBasic;
use App\Models\CompanyJob;
use App\Models\StoreUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Request;
use Redis;
use App\Admin\Actions\Post\FreezeStore;
use App\Admin\Actions\Post\UnFreezeStore;
use App\Admin\Actions\Post\ChangeJobToDelete;
use App\Admin\Actions\Post\ChangeJobToMoney;
use App\Admin\Actions\Post\AllJobAgree;
use App\Admin\Actions\Post\AllJobRefuse;
use App\Admin\Actions\Post\AllJobDelete;
use App\Admin\Actions\Post\AllJobMoney;
use App\Admin\Actions\Post\WorkerInfo;

class StoreUsersController extends AdminController
{
    protected $title = '商户';
    protected $status = [
        '-1' => '冻结',
        '0' => '未审核',
        '1' => '正常',
    ];

    protected function grid()
    {
        $grid = new Grid(new StoreUser());
        $grid->column('sid', 'Id号');
        $grid->column('username', '账号');
        $grid->column('name', '姓名');
        $grid->column('email', 'email');
        $grid->column('mobile', '手机号码');
        $grid->column('status', '状态')->using($this->status);

        $grid->filter(function ($filter) {
            $filter->equal('status', '状态')->select($this->status);
        });

        $grid->model()->orderBy('sid', 'desc');

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $row = $actions->row;

            if (in_array($row->status, [0, 1])) {
                $actions->add(new FreezeStore());
            }

            if (in_array($row->status, [0, -1])) {
                $actions->add(new UnFreezeStore());
            }

        });

        $grid->batchActions(function ($batch) {
            $batch->disabledelete();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(StoreUser::findOrFail($id));

        $show->field('username', '账号');
        $show->field('name', '姓名');
        $show->field('email', 'email');
        $show->field('mobile', '手机号码');

        $show->field('created_at', '提交时间');
        $show->field('updated_at', '更新时间');
        $show->field('status', '状态')->using($this->status);

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new StoreUser());

        //保存前回调
        $form->saving(function (Form $form) {
            $sid = $form->sid;
            $password = $form->password;
            $npassword = '';
            if ($sid > 0) {
                $opassword = StoreUser::where('sid', $sid)->value('password');
                if ($opassword != $password) {
                    $npassword = think_ucenter_md5($password);
                }
            } else {
                $npassword = think_ucenter_md5($password);
            }

            if($npassword){
                $form->password = $npassword;
            }
        });

        $form->text('sid', 'Id号')->readonly();
        $form->text('username', '账号')->required();
        $form->text('name', '姓名')->required();
        $form->mobile('mobile', '手机号码')->required();
        $form->password('password', '密码')->required();
        $form->email('email', 'email')->required();

        return $form;
    }
}
